// memberVip/memberAuthor/memberAuthor.js
var common = require("../../../common/common.js").common;

var leader = require("../../../common/leader.js").leader;

var shoppingCar = require("../../../common/shoppingCar.js");

var util = require("../../../utils/util.js");

var app = getApp();

var {$author: $author} = require("../../../components/base/authorization.js");

Page({
    /**
   * 页面的初始数据
   */
    data: {
        vipStatus: "",
        save_money: "",
        goods: [],
        tmplIds: [],
        //模板消息tmplIds
        subscribeMessage: true,
        showLoading: 0,
        allBottom: 1,
        page: 1,
        allshen: [],
        info: [],
        infoid: "",
        infos: [],
        // goTovip: true,
        rulest: true,
        experience: "",
        count: "",
        l: "",
        pay: [],
        huiyuan1: false,
        huiyuan2: false,
        huiyuan3: false,
        huiyuan4: false,
        huiyuan5: false,
        huiyuan6: false,
        lent: "",
        //是否开启付费购买
        is_try_vip: "",
        //是否开启消费赠卡
        loadMore: true,
        minimum_num: 1,
        step_num: 1,
        joinCate: 1
    },
    //获取去右侧胶囊的信息
    getHeader() {
        let menuRect = wx.getMenuButtonBoundingClientRect();
        this.setData({
            topBarHeight: menuRect.height,
            topBarTop: menuRect.top
        });
    },
    //获取用户信息
    getUsersInfomation: function() {
        var that = this;
        var openid = wx.getStorageSync("user").openid;
        that.taocan();
        common.request({
            endpoint: "My/myinfo/myinfo",
            method: "post",
            data: {
                openid: openid
            },
            success: res => {
                that.setData({
                    info: res.extra,
                    infoid: res.extra.m_id
                });
            }
        });
    },
    /**
   * 生命周期函数--监听页面加载
   */
    onLoad: function(options) {
        this.getHeader();
        let that = this;
        if (options.scene) {
            var scene = decodeURIComponent(options.scene);
            var s_leader = scene.split(":");
            that.setData({
                domain: app.globalData.domain.slice(0, -10),
                experience: s_leader[3],
                count: s_leader[5],
                l: s_leader[1]
            });
        } else {
            that.setData({
                domain: app.globalData.domain.slice(0, -10),
                experience: options.ex,
                count: options.count,
                l: options.l
            });
        }
        that.getUsersInfomation();
        if (that.data.l != "") {
            that.oldname();
        }
        //首次直接进入，拿openid
                if (app.globalData.user) {
            //如果直接拿到openid
            that.requestStart(app.globalData.user.openid);
            that.setData({
                openid: app.globalData.user.openid
            });
        } else {
            //回调拿到openid
            app.userInfoReadyCallback = (res => {
                that.requestStart(res.openid);
            });
        }
        common.request({
            endpoint: "vipcard/vip-verify",
            method: "POST",
            data: "",
            success: res => {
                that.setData({
                    vipStatus: res.extra.vip_card,
                    save_money: res.extra.save_money
                });
            }
        });
    },
    /**
   * 生命周期函数--监听页面初次渲染完成
   */
    onReady: function() {},
    /**
   * 生命周期函数--监听页面显示
   */
    onShow: function() {
        var that = this;
        that.recent();
        leader.getMyLeaderInfo(that.data.openid, that.data.leader_id);
        //公用方法团长请求
                wx.getStorage({
            key: "leaderRefresh",
            success(res) {
                if (!!res.data) {
                    //有缓存且不为0（leaderRefresh=1)
                    //刷新整个首页
                    that.requestStart();
                    //缓存开关变成0
                                        wx.setStorage({
                        key: "leaderRefresh",
                        data: 0
                    });
                }
            }
        });
        common.getCart();
        var newPro = that.changeProInfo(that.data.goods);
        var newShoppingCart = shoppingCar.getInfo();
        that.setData({
            cart_count: newShoppingCart.cart_count,
            goods: newPro
        });
    },
    /**
   * 生命周期函数--监听页面隐藏
   */
    onHide: function() {},
    /**
   * 生命周期函数--监听页面卸载
   */
    onUnload: function() {},
    /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
    onPullDownRefresh: function() {
        this.getUsersInfomation();
        common.request({
            endpoint: "vipcard/vip-verify",
            method: "POST",
            data: "",
            success: res => {
                this.setData({
                    vipStatus: res.extra.vip_card,
                    save_money: res.extra.save_money
                });
            }
        });
        wx.stopPullDownRefresh();
    },
    /**
   * 页面上拉触底事件的处理函数
   */
    onReachBottom: function() {
        let that = this;
        if (that.data.loadMore) {
            that.setData({
                loadMore: false
            });
            that.getGoods();
        }
    },
    /**
   * 用户点击右上角分享
   */
    onShareAppMessage: function() {},
    // 权益渲染
    taocan: function() {
        var that = this;
        common.request({
            endpoint: "vipcard/vip-info",
            method: "POST",
            data: {},
            success: res => {
                that.setData({
                    vipMethods: res.extra.vip_info
                });
                if (res.extra.vip_profit.profit_type.length > 0) {
                    var words = res.extra.vip_profit.profit_type.split(",");
                    for (var i = 0; i < words.length; i++) {
                        var numb = words[i];
                        if (numb == 1) {
                            that.setData({
                                huiyuan1: true
                            });
                        }
                        if (numb == 2) {
                            that.setData({
                                huiyuan2: true
                            });
                        }
                        if (numb == 3) {
                            that.setData({
                                huiyuan3: true
                            });
                        }
                        if (numb == 4) {
                            that.setData({
                                huiyuan4: true
                            });
                        }
                        if (numb == 5) {
                            that.setData({
                                huiyuan5: true
                            });
                        }
                        if (numb == 6) {
                            that.setData({
                                huiyuan6: true
                            });
                        }
                    }
                }
                let rule = "";
                if (res.extra.vip_set.rule == "" || res.extra.vip_set.rule == null || res.extra.vip_set.rule == undefined) {
                    rule = "暂无规则";
                } else {
                    rule = res.extra.vip_set.rule;
                }
                let name = "";
                if (res.extra.vip_set.name == "" || res.extra.vip_set.name == null || res.extra.vip_set.name == undefined) {
                    name = "--";
                } else {
                    name = res.extra.vip_set.name;
                }
                that.setData({
                    pay: res.extra.vip_profit,
                    rule: rule,
                    lent: res.extra.vip_info,
                    is_try_vip: res.extra.vip_set.is_try_vip,
                    name: name,
                    background_type: res.extra.vip_set.background_type,
                    background: res.extra.vip_set.background
                });
            }
        });
    },
    reduce_count: function() {
        let buy_count = this.data.buy_count;
        if (buy_count > 1) {
            buy_count--;
        }
        this.setData({
            buy_count: buy_count
        });
    },
    // 生成二维码用户信息
    oldname: function() {
        var that = this;
        common.request({
            endpoint: "vipcard/vip-member",
            method: "post",
            data: {
                uid: that.data.l
            },
            success: res => {
                that.setData({
                    infos: res.extra
                });
            }
        });
    },
    requestStart: function(openid) {
        let that = this;
        leader.getMyLeaderInfo(that.data.openid, that.data.leader_id);
        //公用方法团长请求
                that.getBase();
        setTimeout(() => {
            that.getGoods();
        }, 1500);
    },
    //全局基础配置信息
    getBase: function() {
        var that = this;
        common.request({
            endpoint: "set/app_base",
            data: {},
            success: res => {
                that.setData({
                    color_main: res.extra.color_main,
                    color_minor: res.extra.color_minor,
                    apptitle: res.extra.title,
                    style: res.extra.style,
                    leader_change: res.extra.l_change,
                    phone: res.extra.phone,
                    copyImg: res.extra.copyright_img,
                    l_supply: res.extra.l_supply,
                    rp_name: res.extra.rp_name,
                    leaderName: res.extra.leader_title ? res.extra.leader_title : "团长",
                    new_member_audit: res.extra.new_member_audit,
                    userAuditStatus: res.extra.userAuditStatus,
                    special_custom_name: res.extra.special_custom_name
                });
                //处理模块开关
                                var features = common.getModulesSwich(res.extra.features);
                //处理数据格式
                                that.setData({
                    features: features
                });
            }
        });
    },
    goToDetail(e) {
        wx.navigateTo({
            url: e.currentTarget.dataset.url
        });
    },
    goToDetail1(e) {
        wx.switchTab({
            url: e.currentTarget.dataset.url
        });
    },
    getGoods: function() {
        var that = this;
        that.setData({
            showLoading: 1,
            allBottom: 0
        });
        common.request({
            endpoint: "vipcard/vip-product",
            method: "post",
            data: {
                page: that.data.page,
                leader_id_true: that.data.myleaderinfo.leader_id_true
            },
            success: res => {
                if (res.extra.length < 1) {
                    that.setData({
                        allBottom: 2,
                        showLoading: 0
                    });
                } else {
                    var proInfo = that.changeProInfo(res.extra);
                    let goods = that.data.page == 1 ? proInfo : that.data.goods.concat(proInfo);
                    that.setData({
                        goods: goods,
                        page: that.data.page + 1,
                        showLoading: 0
                    });
                }
                that.setData({
                    loadMore: true
                });
            },
            fail() {
                that.setData({
                    loadMore: true
                });
            }
        });
    },
    /**
   * 根据 sku 获取物品
   */
    getProductBySku: function(sku_products, sku) {
        return sku_products[sku];
    },
    close_alert() {
        this.setData({
            alert: 0
        });
    },
    buy: function(e) {
        var that = this;
        //不是会员
                if (that.data.vipStatus <= 0) {
            //付费会员才可以购买
            if (e.detail.onlyvip == 1) {
                wx.showModal({
                    title: "提示",
                    content: "该商品仅限会员购买，请先成为会员",
                    confirmText: "去升级",
                    success(res) {
                        if (res.confirm) {
                            wx.redirectTo({
                                url: "/memberVip/pages/memberAuthor/memberAuthor"
                            });
                            return;
                        } else {
                            return;
                        }
                    }
                });
                return;
            }
        }
        var alert = that.data.alert;
        var details = that.data.details;
        var sku_info = that.data.sku_info;
        var buy_count = that.data.buy_count;
        var id = that.data.goodsId;
        var sku_id = sku_info.item_id;
        let sku_item = sku_info.item_id + "," + buy_count + "," + sku_info.credit_sent + "," + details.only_express;
        var price = "";
        if (e.currentTarget.dataset.price) {
            price = e.currentTarget.dataset.price;
        }
        if (sku_info.stock_number >= buy_count) {
            wx.navigateTo({
                url: "/newPlace_order/pages/newPlace_order/newPlace_order?sku_item=" + sku_item + "&status=" + 0 + "&newuserprice=" + price
            });
            that.setData({
                alert: 0,
                addShoppingCar: true,
                newUserProductPrice: ""
            });
        } else {
            wx.showToast({
                title: "库存不足",
                icon: "none"
            });
            that.setData({
                alert: 0
            });
        }
    },
    swiperChange(e) {
        this.setData({
            currentIndex: e.detail.current
        });
    },
    add_count: function(e) {
        let that = this;
        that.add_count_ch(e);
        if (that.data.subscribeMessage) {
            wx.requestSubscribeMessage({
                tmplIds: that.data.tmplIds,
                success(res) {},
                fail(err) {
                    console.log(err);
                },
                complete() {}
            });
        }
    },
    add_count_ch: function(e) {
        let that = this;
        let restrictions = that.data.addShoppingCar ? e.currentTarget.dataset.restrictions : e.currentTarget.dataset.newlimit;
        let buy_count = this.data.buy_count;
        console.log(restrictions + "," + buy_count);
        if (restrictions == 0) {
            buy_count++;
        } else {
            if (buy_count >= restrictions) {
                wx.showModal({
                    title: "提示",
                    content: "本商品限购" + restrictions + "份"
                });
                buy_count = restrictions;
            } else {
                buy_count++;
            }
        }
        this.setData({
            buy_count: buy_count
        });
    },
    join_cart: function(e) {
        let that = this;
        that.setData({
            joinCate: 1
        });
        //不是会员
                if (that.data.vipStatus <= 0) {
            //付费会员才可以购买
            if (e.detail.onlyvip == 1) {
                wx.showModal({
                    title: "提示",
                    content: "该商品仅限会员购买，请先成为会员",
                    confirmText: "去升级",
                    success(res) {
                        if (res.confirm) {
                            wx.redirectTo({
                                url: "/memberVip/pages/memberAuthor/memberAuthor"
                            });
                            return;
                        } else {
                            return;
                        }
                    }
                });
                return;
            }
        }
        var id = e.detail.id;
        e = {
            currentTarget: {
                dataset: {
                    id: e.detail.id,
                    single: e.detail.single,
                    minimumnum: e.detail.minimumnum,
                    stepnum: e.detail.stepnum
                }
            }
        };
        that.join_cart_current(e);
    },
    //加入购物车 此处商品详情接口需要跳转，并且增加加入中的提示
    join_cart_current(e) {
        var that = this;
        var id = e.currentTarget.dataset.id;
        let single = e.currentTarget.dataset.single;
        common.request({
            endpoint: "product/product_info",
            method: "post",
            data: {
                id: id
            },
            success: res => {
                let details = res.extra;
                details.sku_attribute = details.sku_attribute.map(item => {
                    item.attribute_value = this.selectSingleSku(item.attribute_value);
                    return item;
                });
                let sku_attribute = details.sku_attribute;
                let sku = sku_attribute.reduce((s, v) => {
                    s = this.reBlanceSku(s, parseInt(v.attribute_key.id), parseInt(v.attribute_value[0].id));
                    return s;
                }, "");
                let sku_info = this.getProductBySku(details.sku, sku);
                let cart_count = wx.getStorageSync("cart_count");
                if (single == 1) {
                    that.setData({
                        minimum_num: e.currentTarget.dataset.minimumnum,
                        step_num: e.currentTarget.dataset.stepnum
                    });
                    that.join_cart_current_ch(details, e);
                    if (that.data.subscribeMessage) {
                        wx.requestSubscribeMessage({
                            tmplIds: that.data.tmplIds,
                            success(res) {},
                            fail(err) {
                                console.log(err);
                            },
                            complete() {}
                        });
                    }
                } else {
                    that.setData({
                        details: details,
                        sku: sku,
                        alert: 1,
                        product_id: id,
                        sku_info: sku_info,
                        proId: id,
                        buy_count: 1,
                        spec_fund: details.spec_fund,
                        is_vip: sku_info.is_vip,
                        is_mem_level: sku_info.is_mem_level,
                        vip_price: sku_info.vip_price,
                        mem_level_price: sku_info.mem_level_price
                    });
                }
            }
        });
    },
    //单规格商品购买
    join_cart_current_ch: function(details, e) {
        var that = this;
        var id = e.currentTarget.dataset.id;
        let single = e.currentTarget.dataset.single;
        details.sku_attribute = details.sku_attribute.map(item => {
            item.attribute_value = this.selectSingleSku(item.attribute_value);
            return item;
        });
        let sku_attribute = details.sku_attribute;
        let sku = sku_attribute.reduce((s, v) => {
            s = this.reBlanceSku(s, parseInt(v.attribute_key.id), parseInt(v.attribute_value[0].id));
            return s;
        }, "");
        let sku_info = this.getProductBySku(details.sku, sku);
        // let cart_count = wx.getStorageSync("cart_count")
                if (sku_info.stock_number >= 1) {
            that.cartRequest(sku_info.item_id, "add", id, 1);
        } else {
            wx.showToast({
                title: "库存不足",
                icon: "none"
            });
        }
    },
    cartRequest(sku, type, id, single) {
        console.log(id);
        let that = this;
        let num = 0;
        if (single == 1) {
            var addC = that.data.joinCate == 1 ? parseInt(that.data.minimum_num) : parseInt(that.data.step_num);
            num = type == "add" ? addC : -addC;
        } else {
            num = that.data.buy_count;
        }
        let goods = that.data.goods;
        let i = goods.findIndex(item => item.id == id);
        if (type != "add") {
            if (goods[i].cart_num <= parseInt(that.data.minimum_num)) {
                num = -goods[i].cart_num;
            }
        }
        common.request({
            endpoint: "Order/cart/add",
            method: "post",
            data: {
                leader_id: that.data.myleaderinfo.leader_id != -1 ? that.data.myleaderinfo.leader_id : 0,
                sku_id: sku,
                business_id: 0,
                num: num,
                flag: 0,
                type: 0
            },
            success: res => {
                if (single == 1) {
                    if (type == "add") {
                        goods[i].cart_num += num;
                    } else {
                        goods[i].cart_num += num;
                    }
                } else {
                    goods[i].cart_num += that.data.buy_count;
                }
                if (goods[i].cart_num < 0) {
                    goods[i].cart_num = 0;
                }
                that.setData({
                    goods: goods,
                    alert: 0
                });
                if (that.data.subscribeMessage) {
                    wx.requestSubscribeMessage({
                        tmplIds: that.data.tmplIds,
                        success(res) {},
                        fail(err) {
                            console.log(err);
                        },
                        complete() {}
                    });
                }
            },
            fail: err => {
                wx.showModal({
                    title: "提示",
                    content: err.message
                });
            }
        });
    },
    //获取tmplIds
    getTmplIds: function() {
        let that = this;
        const version = wx.getSystemInfoSync().SDKVersion;
        //console.log(version);
                if (util.compareVersion(version, "2.8.2") < 0) {
            that.setData({
                tmplIds: [],
                subscribeMessage: false
            });
            return;
        }
        common.request({
            endpoint: "set/template_get",
            method: "post",
            data: {},
            header: {
                "content-type": "application/json"
            },
            success: res => {
                console.log(res);
                let tmplIds = [];
                let subscribeMessage = true;
                res.extra.map(v => {
                    if ((v.type == "21" || v.type == "22" || v.type == "23") && v.status == "1") {
                        tmplIds.push(v.template_id);
                    }
                });
                if (!tmplIds.length) {
                    subscribeMessage = false;
                }
                that.setData({
                    tmplIds: tmplIds,
                    subscribeMessage: subscribeMessage
                });
            }
        });
    },
    /**
   * 商品信息修改
   */
    changeProInfo(proInfo, cartInfoParamter) {
        // console.log("商品修改")
        // console.log(proInfo)
        // console.log(proInfo)
        var cartInfo = cartInfoParamter ? cartInfoParamter.statisticsArry : shoppingCar.getInfo().statisticsArry;
        if (proInfo.length > 0) {
            proInfo.forEach((item, index) => {
                if (cartInfo.length > 0) {
                    for (var i = 0; i < cartInfo.length; i++) {
                        if (item.id == cartInfo[i].product_id) {
                            item.buyCount = cartInfo[i].count;
                            break;
                        } else {
                            item.buyCount = 0;
                        }
                    }
                } else {
                    item.buyCount = 0;
                }
            });
        }
        return proInfo;
    },
    //购物车减
    reduceCount(e) {
        let that = this;
        that.setData({
            joinCate: 2
        });
        //不是会员
                if (that.data.vipStatus <= 0) {
            //付费会员才可以购买
            if (e.detail.onlyvip == 1) {
                wx.showModal({
                    title: "提示",
                    content: "该商品仅限会员购买，请先成为会员",
                    confirmText: "去升级",
                    success(res) {
                        if (res.confirm) {
                            wx.redirectTo({
                                url: "/memberVip/pages/memberAuthor/memberAuthor"
                            });
                            return;
                        } else {
                            return;
                        }
                    }
                });
                return;
            }
        }
        if (e.detail.single == 1) {
            var params = {
                id: e.detail.id
            };
            console.log(e);
            common.request({
                // endpoint: 'product/product_info',
                endpoint: "product/shopping-push",
                method: "post",
                data: params,
                success: res => {
                    let details = res.extra;
                    this.setData({
                        commission_show: details.commission_show
                    });
                    details.sku_attribute = details.sku_attribute.map(item => {
                        item.attribute_value = this.selectSingleSku(item.attribute_value);
                        return item;
                    });
                    let sku_attribute = details.sku_attribute;
                    let sku = sku_attribute.reduce((s, v) => {
                        s = this.reBlanceSku(s, parseInt(v.attribute_key.id), parseInt(v.attribute_value[0].id));
                        return s;
                    }, "");
                    let sku_info = this.getProductBySku(details.sku, sku);
                    that.setData({
                        minimum_num: e.detail.minimumnum,
                        step_num: e.detail.stepnum
                    });
                    that.cartRequest(sku_info.item_id, "reduce", e.detail.id, 1);
                }
            });
            // common.changeIconNum(newShoppingCart.cart_count, 2)
                } else {
            wx.showToast({
                title: "该商品是多属性商品，请到购物车操作",
                icon: "none"
            });
        }
    },
    //加
    addCount(e) {
        let that = this;
        that.setData({
            joinCate: 2
        });
        //不是会员
                if (that.data.vipStatus <= 0) {
            //付费会员才可以购买
            if (e.detail.onlyvip == 1) {
                wx.showModal({
                    title: "提示",
                    content: "该商品仅限会员购买，请先成为会员",
                    confirmText: "去升级",
                    success(res) {
                        if (res.confirm) {
                            wx.redirectTo({
                                url: "/memberVip/pages/memberAuthor/memberAuthor"
                            });
                            return;
                        } else {
                            return;
                        }
                    }
                });
                return;
            }
        }
        if (that.data.subscribeMessage) {
            that.addCount_ch(e);
            wx.requestSubscribeMessage({
                tmplIds: that.data.tmplIds,
                success(res) {},
                fail(err) {
                    console.log(err);
                },
                complete() {}
            });
        }
    },
    addCount_ch: function(e) {
        e = {
            currentTarget: {
                dataset: {
                    id: e.detail.id,
                    single: e.detail.single,
                    minimumnum: e.detail.minimumnum,
                    stepnum: e.detail.stepnum
                }
            }
        };
        this.join_cart_current(e);
    },
    /** 
   * 添加 sku 属性
   */
    addSkuAttribute: function(e) {
        let key_id = parseInt(e.currentTarget.dataset.key_id);
        let value_id = parseInt(e.currentTarget.dataset.value_id);
        let a_key_index = parseInt(e.currentTarget.dataset.a_key_index);
        let a_value_index = parseInt(e.currentTarget.dataset.a_value_index);
        let sku = this.data.sku;
        sku = this.reBlanceSku(sku, key_id, value_id);
        let attribute = this.data.details.sku_attribute;
        attribute = this.selectSku(attribute, a_key_index, a_value_index);
        let sku_info = this.getProductBySku(this.data.details.sku, sku);
        this.setData({
            sku: sku,
            ["details.sku_attribute"]: attribute,
            sku_info: sku_info
        });
    },
    /** 
   * 重平衡 sku
   * key_id: integer
   */
    reBlanceSku: function(sku, key_id, value_id) {
        let sku_lib = sku != "" ? this.splitSku(sku) : [];
        sku_lib = this.addSku(sku_lib, {
            key_id: key_id,
            value_id: value_id
        });
        sku_lib = this.sortSku(sku_lib);
        return this.buildSku(sku_lib);
    },
    /** 
   * 重建 sku
   */
    buildSku: function(sku_lib) {
        return sku_lib.map(item => {
            return item.key_id + "," + item.value_id;
        }).join("|");
    },
    /** 
   * 对选择的 sku 属性进行排序
   */
    sortSku: function(sku_lib) {
        return sku_lib.sort((pre, next) => {
            if (pre.key_id > next.key_id) {
                return 1;
            } else if (pre.key_id < next.key_id) {
                return -1;
            } else {
                return 0;
            }
        });
    },
    /** 
   * 分割现有 sku 成对象数组
   */
    splitSku: function(sku) {
        return sku.split("|").map(sku_tmp => {
            let [key, value] = sku_tmp.split(",");
            const t = {};
            t.key_id = parseInt(key);
            t.value_id = parseInt(value);
            return t;
        });
    },
    /** 
   * 添加一个 sku
   */
    addSku: function(sku_lib, sku) {
        let t = sku_lib.filter(item => {
            if (item.key_id === sku.key_id) {
                return false;
            }
            return true;
        });
        t.push(sku);
        return t;
    },
    /** 
   * 选择 sku
   */
    selectSku: function(attribues, a_index, v_index = 0) {
        if (attribues[a_index]) {
            let values = attribues[a_index].attribute_value;
            values = values.map(item => {
                item.selected = false;
                return item;
            });
            values[v_index].selected = true;
            attribues[a_index].attribute_value = values;
        }
        return attribues;
    },
    join_goods: function() {
        let that = this;
        that.cartRequest(that.data.sku_info.item_id, "add", that.data.details.id, 2);
    },
    join_goods_ch: function() {
        var that = this;
        var alert = that.data.alert;
        var cart_count = wx.getStorageSync("cart_count") || 0;
        var details = that.data.details;
        var sku_info = that.data.sku_info;
        var buy_count = that.data.buy_count;
        var id = that.data.product_id;
        cart_count = cart_count + buy_count;
        var cart = wx.getStorageSync("cart") || [];
        if (cart_count <= 99999) {
            if (sku_info.stock_number >= buy_count) {
                if (cart == "") {
                    let goods = {
                        id: sku_info.item_id,
                        product_id: id,
                        img: details.icon_img,
                        title: details.name,
                        varieties: sku_info.title,
                        price: sku_info.price,
                        o_price: sku_info.o_price,
                        count: buy_count,
                        stock_number: sku_info.stock_number,
                        select: 1,
                        proID: id,
                        status: 1,
                        credit_sent: sku_info.credit_sent || 0,
                        spec_fund: details.spec_fund,
                        is_vip: sku_info.is_vip,
                        is_mem_level: sku_info.is_mem_level,
                        vip_price: sku_info.vip_price,
                        mem_level_price: sku_info.mem_level_price
                    };
                    wx.showToast({
                        title: "加入购物车成功",
                        icon: "none",
                        success: res => {
                            wx.setStorage({
                                key: "cart_count",
                                data: cart_count
                            });
                            this.setData({
                                cart_count: cart_count
                            });
                            common.changeIconNum(cart_count, 2);
                            var cart = wx.getStorageSync("cart") || that.data.cart;
                            if (!cart) {
                                cart = [];
                            }
                            cart.push(goods);
                            wx.setStorage({
                                key: "cart",
                                data: cart
                            });
                            setTimeout(() => {
                                let newShoppingCart = shoppingCar.getInfo();
                                var newPro = that.changeProInfo(that.data.goods, newShoppingCart);
                                that.setData({
                                    buy_count: 1,
                                    alert: 0,
                                    goods: newPro
                                });
                            }, 500);
                        }
                    });
                } else {
                    var total = 0;
                    var index = 0;
                    for (var i = 0; i < cart.length; i++) {
                        if (sku_info.item_id != cart[i]["id"]) {
                            total++;
                        } else {
                            index = i;
                            break;
                        }
                    }
                    if (total == cart.length) {
                        let goods = {
                            id: sku_info.item_id,
                            product_id: id,
                            img: details.icon_img,
                            title: details.name,
                            varieties: sku_info.title,
                            price: sku_info.price,
                            o_price: sku_info.o_price,
                            count: buy_count,
                            stock_number: sku_info.stock_number,
                            select: 1,
                            proID: id,
                            status: 1,
                            credit_sent: sku_info.credit_sent || 0,
                            spec_fund: details.spec_fund,
                            is_vip: sku_info.is_vip,
                            is_mem_level: sku_info.is_mem_level,
                            vip_price: sku_info.vip_price,
                            mem_level_price: sku_info.mem_level_price
                        };
                        wx.showToast({
                            title: "加入购物车成功",
                            icon: "none",
                            success: res => {
                                wx.setStorage({
                                    key: "cart_count",
                                    data: cart_count
                                });
                                this.setData({
                                    cart_count: cart_count
                                });
                                common.changeIconNum(cart_count, 2);
                                var cart = wx.getStorageSync("cart") || that.data.cart;
                                cart.push(goods);
                                wx.setStorage({
                                    key: "cart",
                                    data: cart
                                });
                                setTimeout(() => {
                                    let newShoppingCart = shoppingCar.getInfo();
                                    var newPro = that.changeProInfo(that.data.goods, newShoppingCart);
                                    that.setData({
                                        buy_count: 1,
                                        alert: 0,
                                        goods: newPro
                                    });
                                }, 500);
                            }
                        });
                    } else {
                        cart[index]["count"] = cart[index]["count"] + buy_count;
                        wx.showToast({
                            title: "加入购物车成功",
                            icon: "none",
                            success: res => {
                                wx.setStorage({
                                    key: "cart_count",
                                    data: cart_count
                                });
                                this.setData({
                                    cart_count: cart_count
                                });
                                common.changeIconNum(cart_count, 2);
                                wx.setStorage({
                                    key: "cart",
                                    data: cart
                                });
                                setTimeout(() => {
                                    let newShoppingCart = shoppingCar.getInfo();
                                    var newPro = that.changeProInfo(that.data.goods, newShoppingCart);
                                    that.setData({
                                        buy_count: 1,
                                        alert: 0,
                                        goods: newPro
                                    });
                                }, 500);
                            }
                        });
                    }
                }
            } else {
                wx.showToast({
                    title: "加入购物车失败",
                    icon: "none"
                });
            }
        } else {
            wx.showToast({
                title: "购物车最多加99999个商品！",
                icon: "none"
            });
        }
    },
    /** 
   * 选择单个 sku 属性
   */
    selectSingleSku: function(attribute_value, v_index = 0) {
        attribute_value.map(item => {
            item.selected = false;
            return item;
        });
        attribute_value[v_index].selected = true;
        return attribute_value;
    },
    // 大家都在升
    recent() {
        var that = this;
        common.request({
            endpoint: "vipcard/vip-order-recent",
            method: "POST",
            data: {
                page: 1
            },
            success: res => {
                that.setData({
                    allshen: res.extra
                });
            }
        });
    },
    // goTovipPage: function () {
    //   var that = this
    //   that.setData({
    //     goTovip: true
    //   })
    // },
    rulestPage: function() {
        var that = this;
        that.setData({
            rulest: false
        });
    },
    rulestclosePage: function() {
        var that = this;
        that.setData({
            rulest: true
        });
    },
    goToDetailpage: function() {
        let that = this;
        if (that.data.lent == 0) {
            wx.showToast({
                title: "付费购买会员卡未开启或未设置会员卡",
                icon: "none",
                color: "#fff",
                duration: 2e3
            });
        } else {
            wx.navigateTo({
                url: "/memberVip/pages/memberPay/memberPay"
            });
        }
    },
    goToDetailpage1: function() {
        let that = this;
        console.log(that.data.is_try_vip);
        if (that.data.is_try_vip == 0) {
            wx.showToast({
                title: "消费赠卡未开启",
                icon: "none",
                color: "#fff",
                duration: 2e3
            });
        } else if (that.data.is_try_vip == 1) {
            wx.navigateTo({
                url: "/memberVip/pages/memberConsumption/memberConsumption"
            });
        }
    },
    shareVip: function() {
        var that = this;
        common.getUserInfoNew({
            success() {
                common.request({
                    endpoint: "vipcard/share-vip",
                    method: "POST",
                    data: {
                        rec_user: that.data.l
                    },
                    success: res => {
                        wx.showToast({
                            title: "您已经收下厚礼，快去购物吧~",
                            icon: "success",
                            color: "#fff",
                            duration: 2e3
                        });
                        wx.navigateTo({
                            url: "memberVip/pages/memberConsumption/memberConsumption"
                        });
                    },
                    fail: res => {
                        wx.showToast({
                            title: res.message,
                            icon: "none",
                            color: "#fff",
                            duration: 2e3
                        });
                    }
                });
            }
        });
    },
    goToChooseLeader(e) {
        common.getUserInfoNew({
            url: e.currentTarget.dataset.url
        });
    }
});